package com.baidu.bos.service.impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baidu.bos.dao.IDecidedzoneDao;
import com.baidu.bos.dao.INoticebillDao;
import com.baidu.bos.dao.IWorkbillDao;
import com.baidu.bos.domain.Decidedzone;
import com.baidu.bos.domain.Noticebill;
import com.baidu.bos.domain.Staff;
import com.baidu.bos.domain.Workbill;
import com.baidu.bos.service.INoticebillService;
import com.baidu.crm.service.CustomerService;

@Service
@Transactional
public class NoticebillServiceImpl implements INoticebillService {
	@Resource
	private INoticebillDao noticebillDao;
	@Resource
	private IDecidedzoneDao decidedzoneDao;
	@Resource
	private CustomerService customerService;
	@Resource
	private IWorkbillDao workbillDao;
	//保存业务通知单,根据地址找到定区id并实现自动分单
	public void save(Noticebill model) {
		//保存业务通知单
		noticebillDao.save(model);
		//根据地址查询到定区id
		String decidedzoneid = customerService.findDecidedzoneidByAddress(model
				.getPickaddress());
		//根据定区id查询到定区
		Decidedzone decidedzone = decidedzoneDao.findById(decidedzoneid);
		Staff staff = decidedzone.getStaff();
		model.setStaff(staff);
		model.setOrdertype("自动");
		//为当前取派员创建一个工单
		Workbill workbill = new Workbill();
		workbill.setAttachbilltimes(0);//设置追单次数
		workbill.setBuildtime(new Timestamp(System.currentTimeMillis()));//工单创建时间
		workbill.setNoticebill(model);//关联业务通知单
		workbill.setPickstate("未取件");//取件状态:未取件,取件中,已取件
		workbill.setRemark(model.getRemark());//备注信息
		workbill.setStaff(staff);//工单关联取派员
		workbill.setType("新");//工单类型:新,追,改,销
		workbillDao.save(workbill);//保存工单
	}

}
